package com.bida.controller.admin;

import com.bida.constant.JwtClaimsConstant;
import com.bida.domain.dto.EmployeeDTO;
import com.bida.domain.dto.EmployeeLoginDTO;
import com.bida.domain.dto.PasswordEditDTO;
import com.bida.domain.entity.Employee;
import com.bida.domain.vo.EmployeeLoginVO;
import com.bida.properties.JwtProperties;
import com.bida.result.PageResult;
import com.bida.result.Result;
import com.bida.service.IEmployeeService;
import com.bida.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }


    /**
     * 新增员工
     * @param employeeDTO
     * @return
     */
    @PostMapping
    public Result saveNewEmployee(@RequestBody EmployeeDTO employeeDTO){
        System.out.println(employeeDTO);
        return employeeService.saveNewEmployee(employeeDTO);
    }

    /**
     * 员工分页查询
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> pageGetEmployee(String name, Integer page, Integer pageSize){
        return employeeService.pageGetEmployee(name, page, pageSize);
    }

    /**
     * 启用、禁用员工账号
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable("status")Integer status,Long id){
        return employeeService.updateStatus(status,id);
    }

    /**
     * 编辑员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping
    public Result updateEmployeeInfo(@RequestBody EmployeeDTO employeeDTO){
        return employeeService.updateEmployeeInfo(employeeDTO);
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Employee> getEmployeeInfoById(@PathVariable("id") Long id){
        return employeeService.getEmployeeInfoById(id);
    }

    /**
     * 修改密码
     * @param passwordEditDTO
     * @return
     */
    @PutMapping("/editPassword")
    public Result eitPasswordById(@RequestBody PasswordEditDTO passwordEditDTO){
        return employeeService.eitPasswordById(passwordEditDTO);
    }


}
